﻿using lwzlz.Application.System;
using lwzlz.Common;
using lwzlz.Core;
using lwzlz.Web.Areas.SysAdmin.Models;
using lzwlz.Web;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.Mall.Controllers
{
    public class StudentController : MallController
    {
        #region 界面
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            return View();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit(string id = "")
        {
            var model = new Student();

            if (!string.IsNullOrWhiteSpace(id) && int.TryParse(id, out int idInt))
            {
                model = new StudentService().Get(idInt);
                model.PassWord = new Symmetric().Decrypto(model.PassWord);
            }

            return View(model);
        }
        #endregion

        #region 接口
        /// <summary>
        /// 账号列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult StudentList(int pageNumber, int pageSize, string keyword = null)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var studentService = new StudentService();
                var dic = new Dictionary<string, string>();
                //会员ID
                {
                    dic.Add("MemberId", MallMemberLogin.GetCurrentUser().Id.ToString());
                }
                //关键词
                if (!string.IsNullOrWhiteSpace(keyword))
                {
                    dic.Add("UserName", keyword);
                }

                var list = studentService.GetListPage(pageNumber, pageSize, dic, null, null, out int count);

                response.Data = new { List = list, Count = count, };
                response.Success = true;
                response.Message = "获取成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="UserName"></param>
        /// <param name="PassWord"></param>
        /// <param name="RealName"></param>
        /// <param name="Status"></param>
        /// <param name="MemberId"></param>
        /// <returns></returns>
        public JsonResult Save(string Id, string UserName, string PassWord, string RealName, string Phone, int Status, string MemberId)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var studentService = new StudentService();
                if (!string.IsNullOrWhiteSpace(Id) && int.TryParse(Id, out int id) && id > 0)
                {
                    var entity = studentService.Get(id);
                    entity.UserName = UserName;
                    entity.PassWord = new Symmetric().Encrypto(PassWord);
                    entity.RealName = RealName;
                    entity.Phone = Phone;
                    entity.Status = Status;
                    entity.LastModificationTime = DateTime.Now;
                    var result = studentService.Update(entity, null);
                    if (result > 0)
                    {
                        response.Success = true;
                        response.Message = "保存成功";
                        response.Data = Url.Action("List", "Student");
                    }
                    else
                    {
                        throw new Exception("保存失败");
                    }
                }
                else
                {
                    var entity = new Student()
                    {
                        UserName = UserName,
                        PassWord = new Symmetric().Encrypto(PassWord),
                        RealName = RealName,
                        Phone = Phone,
                        Status = Status,
                        MemberId = int.Parse(MemberId),
                        CreationTime = DateTime.Now
                    };
                    var result = studentService.Insert(entity, null);
                    if (result.HasValue && result.Value > 0)
                    {
                        response.Success = true;
                        response.Message = "保存成功";
                        response.Data = Url.Action("List", "Student");
                    }
                    else
                    {
                        throw new Exception("保存失败");
                    }
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }

        /// <summary>
        /// 删除账户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DeleteStudent(string id)
        {
            ResponseModel response = new ResponseModel();
            try
            {
                var studentService = new StudentService();
                studentService.DeleteList(id.ToString());
                response.Success = true;
                response.Message = "删除成功";
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }
        #endregion
    }
}